package calendar;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 * Implements a graphical calendar that can return a date in the correct Date
 * format.
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class VisualCalendar extends javax.swing.JDialog {

    private MouseAdapter mouseAdapter;
    private ActionListener action1;
    private ActionListener action2;
    private Calendar calendar;
    private static int year;
    private static int month;
    private static int monthLength;
    private String output;

    /** Creates new form VisualCalendar
     * @param parent
     * @param modal
     */
    public VisualCalendar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        calendar = Calendar.getInstance();
        actionListeners();
        setCurrentDate();
        setDays();
        drawCalendar();
        jPanel1.setLayout(new FlowLayout(0, 2, 2));
        jButton1.addActionListener(action1);
        jButton2.addActionListener(action2);
    }

    /**
     * Draws the calendar on the given panel with the number of panels stated
     * by the daysInMonth variable. Adds the mc mouselistener aswell,
     * implementing the funtion to get output from the panel.
     */
    private void drawCalendar() {
        for (int i = 0; i < monthLength; i++) {
            JPanel panel = new JPanel();
            JLabel label = new JLabel();

            label.setText((i + 1) + "");

            panel.setVisible(true);
            panel.setBackground(Color.GREEN);
            panel.setPreferredSize(new Dimension(35, 35));
            panel.add(label, BorderLayout.CENTER);
            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            panel.addMouseListener(mouseAdapter);

            jPanel1.add(panel);
        }

        /**
         * Switches the month number into the full format of the given month and
         * sets the label to the correct month and year.
         */
        switch (month) {
            case 0:
                jLabel1.setText("January " + year);
                break;
            case 1:
                jLabel1.setText("Febuary " + year);
                break;
            case 2:
                jLabel1.setText("March " + year);
                break;
            case 3:
                jLabel1.setText("April " + year);
                break;
            case 4:
                jLabel1.setText("May " + year);
                break;
            case 5:
                jLabel1.setText("June " + year);
                break;
            case 6:
                jLabel1.setText("July " + year);
                break;
            case 7:
                jLabel1.setText("August " + year);
                break;
            case 8:
                jLabel1.setText("September " + year);
                break;
            case 9:
                jLabel1.setText("October " + year);
                break;
            case 10:
                jLabel1.setText("November " + year);
                break;
            case 11:
                jLabel1.setText("December " + year);
                break;
        }
    }

    /**
     * The method that is called when the number of days in the given
     * month is needed.
     */
    private void setDays() {
        monthLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     *
     * @return Returns the date as a String, from the clicked panel.
     */
    public String getDate() {
        return output;
    }

    /**
     * Method to make the calendar invisible.
     */
    private void close() {
        setVisible(false);
    }

    /**
     * Method that implements actionlisteners to the panels and the two buttons.
     * The listener 'mouseAdapter' implements the funtionality to get the proper output from
     * the day panels when they are pressed.
     * The listener 'action1' implements the functionality to scroll back in the
     * months and years.
     * The listener 'action2' implements the funtionality to scroll forward in the
     * months and the years
     */
    private void actionListeners() {
        mouseAdapter = (new java.awt.event.MouseAdapter() {

            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                panelMousePressed(evt);
            }

            private void panelMousePressed(MouseEvent evt) {
                JPanel panel = (JPanel) evt.getSource();
                JLabel label = (JLabel) panel.getComponent(0);
                int day = Integer.parseInt(label.getText());
                String monthOutput = "";
                String dayOutput = "";
                if (day < 10) {
                    dayOutput = "0" + day;
                }
                if (day >= 10) {
                    dayOutput = "" + day;
                }
                if (month < 9) {
                    monthOutput = "0" + (month + 1);
                }
                if (month >= 9) {
                    monthOutput = "" + (month + 1);
                }
                output = year + "-" + monthOutput + "-" + dayOutput;
                //After the output is made, the dialogs visibility is set to false.
                close();
            }
        });

        action1 = (new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                buttonActionPerformed(e);
            }

            private void buttonActionPerformed(ActionEvent e) {
                if (month == 0) {
                    month = 11;
                    year -= 1;
                    calendar.set(year, month, 1);
                    jPanel1.removeAll();
                    jPanel1.paint(jPanel1.getGraphics());
                    setDays();
                    drawCalendar();
                } else {
                    month -= 1;
                    calendar.set(year, month, 1);
                    jPanel1.removeAll();
                    jPanel1.paint(jPanel1.getGraphics());
                    setDays();
                    drawCalendar();
                }
            }
        });

        action2 = (new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                buttonActionPerformed(e);
            }

            private void buttonActionPerformed(ActionEvent e) {
                if (month == 11) {
                    year += 1;
                    month = 0;
                    calendar.clear();
                    calendar.set(year, month, 1);
                    jPanel1.paint(jPanel1.getGraphics());
                    jPanel1.removeAll();
                    setDays();
                    drawCalendar();
                } else {
                    month += 1;
                    calendar.clear();
                    calendar.set(year, month, 1);
                    jPanel1.paint(jPanel1.getGraphics());
                    jPanel1.removeAll();
                    setDays();
                    drawCalendar();
                }
            }
        });
    }

    /**
     * Initialises the calendar to the current date and month.
     */
    private void setCurrentDate() {
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        calendar.set(year, month, 1);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Hotel Lazy BMS - Select date");

        jPanel1.setMaximumSize(new java.awt.Dimension(300, 205));
        jPanel1.setMinimumSize(new java.awt.Dimension(300, 205));
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 205));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 205, Short.MAX_VALUE)
        );

        jLabel1.setText("jLabel1");

        jButton1.setText("<<");

        jButton2.setText(">>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                VisualCalendar dialog = new VisualCalendar(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
